package ThreadRunableVolite;

import org.apache.log4j.Logger;

/**
 * @author Ajie
 * @date 2020-12-24
 * @function
 * 测试 一个 对象，被多个线程回调，回调后的线程
 *
 * 结论：
 * 回调后的 线程 == 传入的工作线程。
 *
 */
public class ThreadObjCallBack {
    private CallBack mCallback;
    public ThreadObjCallBack(ThreadObjCallBack.CallBack callBack) {
        mCallback = callBack;
    }

    private static final Logger logger = Logger.getLogger(ThreadObjCallBack.class);
    public static void main(String[] args) {

        ThreadObjCallBack obj = new ThreadObjCallBack(new CallBack() {
            @Override
            public void onCallBack(String str) {
                logger.info("onCallBack: "+Thread.currentThread().getName()+"  :"+str);
            }
        });

        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    obj.mCallback.onCallBack("thread 1");
                    try {
                        Thread.sleep(1000 * 2);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        },"t-1").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    obj.mCallback.onCallBack("thread 2");
                    try {
                        Thread.sleep(1000 * 2);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        },"t-2").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    obj.mCallback.onCallBack("thread 3");
                    try {
                        Thread.sleep(1000 * 2);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        },"t-3").start();

    }

    interface CallBack{
        void onCallBack(String str);
    }
}
